CYLS    EQU 10 ;柱面数
OS_ADDR EQU 0xC200 ;系统启动地址

    ORG 0x7C00 # 制定内存读取地址
    
    ;FAT12软盘标准格式头
    JMP  entry   ;跳转偏移到50处执行
    DB   0x90
    DB   "MagicIPL"
    DW   512
    DB   1
    DW   1
    DB   2
    DW   224
    DW   2880
    DB   0xf0
    DW   9
    DW   18
    DW   2
    DD   0
    DD   2880
    DB   0,0,0x29
    DD   0xffffffff
    DB   "Magic QP OS"
    DB   "FAT12   "
    RESB 18

entry:
    MOV AX,0
    MOV SS,AX
    MOV SP,0x7C00
    MOV DS,AX
    MOV ES,AX

    ;清除屏幕
    MOV AH,0x06
    MOV BH,0x07
    MOV CX,0
    MOV DH,0x18
    MOV DL,0X4F
    INT 0x10

    ;打印欢迎信息
    MOV AX,msg
    MOV DH,0
    MOV CX,18
    CALL print
    
    ;读取磁盘
    MOV AX,0x0820
    MOV BX,0x00
    MOV ES,AX   ;设置内存加载后的地址 ES:BX  ES*16+BX
    MOV DH,0    ;磁头0
    MOV CH,0    ;柱面0
    MOV CL,2    ;扇区2

readloop:
    MOV SI,0
    
retry:
    MOV AH,0x02
    MOV AL,1
    MOV BX,0
    MOV DL,0x00 ;指定为软盘，00H~7FH:软盘驱动器号；80H~0FFH:硬盘驱动器号
    INT 0x13
    JNC next
    
    ADD SI,1
    CMP SI,5    ;读取5次失败，就跳转报错
    JAE error

    ;复位磁盘,重新读取
    MOV AH,0x00
    MOV DL,0X00
    INT 0x13
    JMP retry

next:
    MOV AX,ES
    ADD AX,0x0020 ;偏移一个扇区(512字节)
    MOV ES,AX

    ADD CL,1
    CMP CL,18
    JBE readloop  ;先读取18个扇区

    MOV CL,1
    ADD DH,1
    CMP DH,2
    JB readloop ;上面判断，如果DH小于2就跳转的readloop

    MOV DH,0
    ADD CH,1
    CMP CH,CYLS ;读取10个柱面
    JB readloop

    MOV [0X0FF0],CH
    JMP success
fin:
    HLT
    JMP fin
print:
    MOV BP,AX
    MOV AX,0x01301
    MOV BX,0x0007
    MOV DL,0
    INT 0x10
    RET
success:
    MOV AX,0
    MOV ES,AX
    MOV AX,sdata
    MOV CX,15
    MOV DH,1
    CALL print
    JMP OS_ADDR

error:
    MOV AX,0
    MOV ES,AX
    MOV AX,edata
    MOV CX,14
    MOV DH,1
    CALL print
    JMP fin

msg:
    DB "ENTRY Magic QP OS"
    DB 0x0a

edata:
    DB "READ DISK FAIL"

sdata:
    DB "LOAD OS SUCCESS"

    RESB 510-($-$$)
    DW 0xAA55

    DB 0xF0,0xFF
    RESB 4606
    DB 0xF0,0xFF,0xFF
    RESB 1469437